source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/Energy Gini Merge/energy_gini.R')
dir
wd = gsub("Energy Gini Merge", "OECD")
source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/Energy Gini Merge/energy_gini.R')
source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/Energy Gini Merge/energy_gini.R')
source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/Energy Gini Merge/energy_gini.R')
source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/Energy Gini Merge/energy_gini.R')
View(oecd)
grep("before tax" oecd$MEASURE)
grep("before tax", oecd$MEASURE)
View(oecd)
grep("before tax", oecd$Measure)
source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/Energy Gini Merge/energy_gini.R')
View(oecd)
oecd = oecd[MEASURE == "GINIB"]
source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/Energy Gini Merge/energy_gini.R')
source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/Energy Gini Merge/energy_gini.R')
View(oecd)
View(oecd)
plot(oecd$Year, oecd$Value)
hist(oecd$Value)
unique(oecd$`Age group`)
source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/Energy Gini Merge/energy_gini.R')
source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/Energy Gini Merge/energy_gini.R')
View(oecd)
hist(oecd$Value, breaks = 50)
plot(oecd$Year, oecd$Value)
View(oecd)
wd = gsub("Energy Gini Merge", "UN_WIID", dir)
setwd(wd)
source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/Energy Gini Merge/energy_gini.R')
source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/Energy Gini Merge/energy_gini.R')
View(un_wiid)
source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/Energy Gini Merge/energy_gini.R')
View(un_wiid)
hist(un_wiid$Gini)
source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/Energy Gini Merge/energy_gini.R')
un_wiid = un_wiid[is.na(Gini) == F]
un_wiid$Gini = un_wiid$Gini/100
source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/Energy Gini Merge/energy_gini.R')
View(un_wiid)
wd = gsub("Energy Gini Merge", "World Bank", dir)
setwd(wd)
source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/Energy Gini Merge/energy_gini.R')
View(oecd)
t = c(1, 5, 10)
x = c(1, 2, 5)
time = c(1, 5, 10)
year = c(1, 5, 10)
value = c(1, 2, 5)
year_vec = seq(min(year), max(year), 1)
approx(year, value, year_vec)
estiamte =   approx(year, value, year_vec)
estimate =  approx(year, value, year_vec)
estimate =  data.table( approx(year, value, year_vec) )
View(estimate)
output = cbind(estimate[1], estimate[2])
View(output)
View(estimate)
estimate =  approx(year, value, year_vec)
output = cbind(estimate[1], estimate[2])
View(output)
estimate$x
output = cbind(estimate$x, estimate$y)
View(output)
output = cbind(year = estimate$x, value = estimate$y)
source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/Energy Gini Merge/energy_gini.R')
interpolate(year, value)
View(oecd)
test = oecd[, interpolate(Year, Value), by = LOCATION]
interpolate = function(year, value){
if(length(year) == 1){
output = cbind(year = year, value = value)
} else{
year_vec = seq(min(year), max(year), 1)
estimate =  approx(year, value, year_vec)
output = cbind(year = estimate$x, value = estimate$y)
}
return(output)
}
test = oecd[, interpolate(Year, Value), by = LOCATION]
View(test)
test = oecd[, as.list(interpolate(Year, Value) ), by = LOCATION]
source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/Energy Gini Merge/energy_gini.R')
test = oecd[, interpolate(Year, Value) , by = LOCATION]
View(test)
View(wdi)
View(test)
View(output)
table(test$LOCATION)
test = oecd[, .(interpolate(Year, Value)) , by = LOCATION]
test = oecd[, list(interpolate(Year, Value)) , by = LOCATION]
test = oecd[, as.list(interpolate(Year, Value)) , by = LOCATION]
test = oecd[, as.list(interpolate(Year, Value)) , by = LOCATION]
interpolate = function(year, value){
if(length(year) == 1){
output = cbind(year = year, value = value)
} else{
year_vec = seq(min(year), max(year), 1)
estimate =  approx(year, value, year_vec)
output = cbind(year = estimate$x, value = estimate$y)
}
return(list(output))
}
test = oecd[, as.list(interpolate(Year, Value)) , by = LOCATION]
test = oecd[, list(interpolate(Year, Value)) , by = LOCATION]
View(test)
test = oecd[, (interpolate(Year, Value)) , by = LOCATION]
interpolate = function(year, value){
if(length(year) == 1){
output = cbind(year = year, value = value)
} else{
year_vec = seq(min(year), max(year), 1)
estimate =  approx(year, value, year_vec)
output = cbind(year = estimate$x, value = estimate$y)
}
return(as.list(output))
}
test = oecd[, (interpolate(Year, Value)) , by = LOCATION]
test = oecd[, list(interpolate(Year, Value)) , by = LOCATION]
View(test)
interpolate = function(year, value){
if(length(year) == 1){
output = cbind(year = year, value = value)
} else{
year_vec = seq(min(year), max(year), 1)
estimate =  approx(year, value, year_vec)
output = cbind(year = estimate$x, value = estimate$y)
}
return(output)
}
test = oecd[, list(interpolate(Year, Value)) , by = LOCATION]
test = oecd[, (interpolate(Year, Value)) , by = LOCATION]
View(oecd)
View(test)
interpolate = function(year, value){
if(length(year) == 1){
output = cbind(year = year, value = value)
} else{
year_vec = seq(min(year), max(year), 1)
estimate =  approx(year, value, year_vec)
output = cbind(year = estimate$x, value = estimate$y)
}
return(list(output))
}
test = oecd[, (interpolate(Year, Value)) , by = LOCATION]
source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/Energy Gini Merge/energy_gini.R')
View(test)
test$LOCATION
test$V1
test$V1[1]
test$V1[1]
test$V1[[1]]
look = test$V1
look[[1]]
look[[1]]
look[[2]]
look[[3]]
View(oecd)
test$V1[[1]]
unlist( test$V1[[1]] )
g = unlist( test$V1[[1]] )
View(g)
test$LOCATION
g = unlist( test )
do.call(rbind.data.table, test$V1)
do.call(rbind, test$V1)
g = do.call(rbind, test$V1)
View(g)
source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/Energy Gini Merge/energy_gini.R')
length(test$V1)
lengths(test$V1)
oecd_interp = oecd[, list(interpolate(Year, Value)) , by = LOCATION]
View(oecd)
energy_interp = wdi[, list(interpolate(year, EG.USE.PCAP.KG.OE)) , by = country.code]
energy = wdi[is.na(wdi$EG.USE.PCAP.KG.OE == F)]
energy = wdi[is.na(wdi$EG.USE.PCAP.KG.OE == F)]
energy_interp = energy[, list(interpolate(year, EG.USE.PCAP.KG.OE)) , by = country.code]
View(energy)
energy = wdi[is.na(wdi$EG.USE.PCAP.KG.OE == F)]
View(energy)
energy = wdi[is.na(wdi$EG.USE.PCAP.KG.OE) == F]
energy_interp = energy[, list(interpolate(year, EG.USE.PCAP.KG.OE)) , by = country.code]
observation = lengths(energy_interp$V1)
g = do.call(rbind, energy_interp$V1)
View(g)
View(energy)
library(data.table)
library(here)
library(magrittr)
dir = here()
wd = paste(dir, "/raw", sep = "")
setwd(wd)
meta = fread("WDISeries.csv")
codes = fread("codes.csv")
country.only = fread("country_only_codes.csv")
d = fread("WDIData.csv", header = F)
info = d[1,] %>% t() %>% as.vector()
d = d[V2 %in% country.only$Code]
series.codes = d[,4] %>% t() %>% as.vector()
ids = which(series.codes %in% codes$code)
y = as.numeric(info)
y = y[-c(1:4)]
keep = d[ids, ]
sub = keep[V4 == codes$code[1]]
n = ncol(sub)-4
country = rep(sub$V1, each = n)
country.code = rep(sub$V2, each = n)
year = rep(y, nrow(sub))
output = data.table(country, country.code, year)
for(i in 1:nrow(codes)){
s.code = codes$code[i]
sub = keep[V4 == s.code]
values = sub[, 5:ncol(sub)]
long = values %>% t() %>% as.vector() %>% as.numeric()
output = cbind(output, long)
name = names(output)
name = gsub("long", s.code, name)
names(output) = name
}
output = output[order(country.code, year),]
View(output)
names(output)
names(output) = c("country", "code3", "year", "energy_pc_GJ", "gini", "gdp_pc")
names(output) = c("country", "code3", "year", "energy_pc_gj", "gini", "gdp_pc")
output$gini = output$gini/100
kcal_to_gj = 0.000004186798
kgoe_to_gj =  0.041868
output$energy_pc_gj = output$energy_pc_gj*kgoe_to_gj + 2000*365*kcal_to_gj
hist(output$energy_pc_gj)
2000*365*kcal_to_gj
View(output)
source('~/Desktop/origin_inequality/Supplementary Material/Empirical Data/World Bank/format.R')
